package com.assistant;

import com.assistant.SELECT.ChooseFileType;
import com.assistant.hanleTask.MakeLogApk;
import com.assistant.hanleTask.ReplaceSdkVersion;
import com.assistant.utils.CommonComponent;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

public class UIHandler {
    private static JTextField sourcePath;
    private static JTextField sdkPath;
    private static JLabel text;

    public static void placeComponents(JPanel jPanel) {
        jPanel.setLayout(null);
        addLT(jPanel, "源路径", ChooseFileType.FILES, 20, 150);
        addLT(jPanel, "sdk文件路径", ChooseFileType.DIRECTORY, 60, 150);
        JButton quickGetLogApk = new JButton("一键日志包");
        JButton replaceSdkVersion = new JButton("一键换版本");
        quickGetLogApk.setBounds(20, 100, 100, 20);
        replaceSdkVersion.setBounds(150, 100, 100, 20);
        quickGetLogApk.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JProgressBar jProgressBar = CommonComponent.createProcessBar();
                MakeLogApk sdkHandler = new MakeLogApk(sourcePath, jPanel, jProgressBar);
                new Thread(sdkHandler).start();
            }
        });
        replaceSdkVersion.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JProgressBar jProgressBar = CommonComponent.createProcessBar();
                ReplaceSdkVersion sdkVersion=new ReplaceSdkVersion(sourcePath,sdkPath,jProgressBar,jPanel);
                new Thread(sdkVersion).start();
            }
        });
        text = new JLabel();
        //text.setBounds(20,140,200,100);
        // jPanel.add(text);
        jPanel.add(quickGetLogApk);
        jPanel.add(replaceSdkVersion);

    }


    /**
     * @param jPanel
     * @param labelName
     * @param type      文件选择框的类型
     * @param y         y坐标位置
     * @param column    jTextField 长度
     */
    //添加一对组件JLabel:JTextField:JButton
    public static void addLT(JPanel jPanel, String labelName, ChooseFileType type, int y, int column) {
        JLabel jLabel = new JLabel(labelName);
        jLabel.setBounds(20, y, 80, 20);
        jPanel.add(jLabel);
        JTextField jTextField = new JTextField(column);
        jTextField.setBounds(120, y, column, 20);
        if (labelName.equals("源路径")) {
            sourcePath = jTextField;
        } else if (labelName.equals("sdk文件路径")) {
            sdkPath = jTextField;
        }
        jPanel.add(jTextField);
        JButton jButton = new JButton("...");
        jButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                //打开文件浏览器
                File selectedFile = CommonComponent.fileChooser(jPanel, null, type, ".apk");
                if (selectedFile != null)
                    if (labelName.equals("源路径")) {
                        sourcePath.setText(selectedFile.getAbsolutePath());
                    } else if (labelName.equals("sdk文件路径")) {
                        sdkPath.setText(selectedFile.getAbsolutePath());
                    }
                ;
            }
        });
        jButton.setBounds(column + 130, y, 40, 20);
        jPanel.add(jButton);
    }
}
